package com.climber.android.commonres.ui.dialog

import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.fragment.app.BaseDialogFragment
import com.climber.android.commonres.R
import io.ganguo.library.mvp.util.StringUtils
import io.ganguo.library.mvp.util.UIUtils

/**
 * Created by Mikes at 2019-11-08 18:25
 */
class CommonVerticalActionsDialog : BaseDialogFragment(){
    companion object {
        fun generate(body: CommonVerticalActionsDialog.() -> CommonVerticalActionsDialog): CommonVerticalActionsDialog {
            return with(CommonVerticalActionsDialog()) {
                body()
            }
        }
    }

    private var title = ""
    private var titleTextColor = UIUtils.getColor(R.color.common_text_primary_dark)
    private var titleTextSize = 17F

    private var content : CharSequence = ""
    private var contentTextColor = UIUtils.getColor(R.color.common_text_primary)
    private var contentTextSize = 15F
    private var contentGravity = Gravity.CENTER

    private var cancelBtnLabel = ""
    private var cancelBtnColor = UIUtils.getColor(R.color.common_text_primary)

    private var confirmBtnLabel = ""
    private var confirmBtnColor = UIUtils.getColor(R.color.common_white_full)

    private var neutralBtnLabel = ""
    private var neutralBtnColor = UIUtils.getColor(R.color.common_white_full)

    private var cancelBtnCallBack: (() -> Unit)? = null
    private var confirmBtnCallBack: (() -> Unit)? = null
    private var neutralBtnCallBack: (() -> Unit)? = null

    private var dialogCancelable = true

    fun setTitle(title: String) {
        this.title = title
    }

    fun setTitleTextSize(textSize : Float){
        this.titleTextSize = textSize
    }

    fun setTitleTextColor(textColor : Int){
        this.titleTextColor = textColor
    }

    fun setContent(content: CharSequence) {
        this.content = content
    }

    fun setContentTextSize(textSize : Float){
        this.contentTextSize = textSize
    }

    fun setContentTextColor(textColor : Int){
        this.contentTextColor = textColor
    }

    fun setContentGravity(gravity : Int){
        this.contentGravity = gravity
    }

    fun setCancelBtnLabel(label: String, callback: (() -> Unit)? = null) {
        this.cancelBtnLabel = label
        this.cancelBtnCallBack = callback
    }

    fun setCancelBtnColor(btnColor : Int) {
        this.cancelBtnColor = btnColor
    }

    fun setConfirmBtnLabel(label: String, callback: (() -> Unit)? = null) {
        this.confirmBtnLabel = label
        this.confirmBtnCallBack = callback
    }

    fun setConfirmBtnColor(btnColor : Int) {
        this.confirmBtnColor = btnColor
    }

    fun setNeutralBtnLabel(label: String, callback: (() -> Unit)? = null) {
        this.neutralBtnLabel = label
        this.neutralBtnCallBack = callback
    }

    fun setNeutralBtnColor(btnColor : Int) {
        this.neutralBtnColor = btnColor
    }

    fun setDialogCancelable(cancelable : Boolean){
        this.dialogCancelable = cancelable
    }

    fun create(): CommonVerticalActionsDialog {
        val appDialog = CommonVerticalActionsDialog()

        val bundle = Bundle()
        bundle.putString("title", this.title)
        bundle.putFloat("titleTextSize", this.titleTextSize)
        bundle.putInt("titleTextColor", this.titleTextColor)

        bundle.putCharSequence("content", this.content)
        bundle.putFloat("contentTextSize", this.contentTextSize)
        bundle.putInt("contentTextColor", this.contentTextColor)
        bundle.putInt("contentGravity", this.contentGravity)

        bundle.putString("cancelBtnLabel", this.cancelBtnLabel)
        bundle.putInt("cancelBtnColor", this.cancelBtnColor)

        bundle.putString("confirmBtnLabel", this.confirmBtnLabel)
        bundle.putInt("confirmBtnColor", this.confirmBtnColor)

        bundle.putString("neutralBtnLabel", this.neutralBtnLabel)
        bundle.putInt("neutralBtnColor", this.neutralBtnColor)

        bundle.putBoolean("dialogCancelable", this.dialogCancelable)

        appDialog.arguments = bundle

        appDialog.cancelBtnCallBack = cancelBtnCallBack
        appDialog.confirmBtnCallBack = confirmBtnCallBack
        appDialog.neutralBtnCallBack = neutralBtnCallBack

        return appDialog
    }

    private lateinit var tvDialogTitle: TextView
    private lateinit var tvDialogBody: TextView

    private lateinit var cancelBtn: TextView
    private lateinit var confirmBtn: TextView
    private lateinit var neutralBtn: TextView

    override fun getLayoutResId(): Int = R.layout.com_dialog_with_vertical_actions

    override fun initView(dialogView: View) {
        tvDialogTitle = dialogView.findViewById(R.id.tvDialogTitle)
        tvDialogBody = dialogView.findViewById(R.id.tvDialogBody)

        cancelBtn = dialogView.findViewById(R.id.cancelBtn)
        confirmBtn = dialogView.findViewById(R.id.confirmBtn)
        neutralBtn = dialogView.findViewById(R.id.neutralBtn)

        cancelBtn.setOnClickListener {
            dismiss()
            cancelBtnCallBack?.invoke()
        }

        confirmBtn.setOnClickListener {
            dismiss()
            confirmBtnCallBack?.invoke()
        }

        neutralBtn.setOnClickListener {
            dismiss()
            neutralBtnCallBack?.invoke()
        }
    }

    override fun initData() {
        val tempArguments = arguments
        if (tempArguments == null) {
            dismiss()
            return
        }

        setAnim(R.style.DialogScaleIn)

        val dialogCancelable = tempArguments.getBoolean("dialogCancelable", true)
        dialog?.run {
            setCancelable(dialogCancelable)
            setCanceledOnTouchOutside(dialogCancelable)
        }

        val title = tempArguments.getString("title", "")
        if(StringUtils.isEmpty(title)){
            tvDialogTitle.visibility = View.GONE
        } else{
            tvDialogTitle.text = title
        }

        tempArguments.getFloat("titleTextSize").run {
            tvDialogTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, this)
        }

        tempArguments.getInt("titleTextColor").run {
            tvDialogTitle.setTextColor(this)
        }

        val content = tempArguments.getCharSequence("content")
        tvDialogBody.movementMethod = LinkMovementMethod.getInstance()
        if(StringUtils.isEmpty(content)){
            tvDialogBody.visibility = View.GONE
        } else{
            tvDialogBody.text = content
        }
        tvDialogBody.gravity = tempArguments.getInt("contentGravity", Gravity.CENTER)

        tempArguments.getFloat("contentTextSize").run {
            tvDialogBody.setTextSize(TypedValue.COMPLEX_UNIT_SP, this)
        }

        tempArguments.getInt("contentTextColor").run {
            tvDialogBody.setTextColor(this)
        }

        val cancelBtnLabel = tempArguments.getString("cancelBtnLabel")
        if(StringUtils.isNotEmpty(cancelBtnLabel)){
            cancelBtn.text = cancelBtnLabel
        } else{
            cancelBtn.visibility = View.GONE
        }

        val cancelBtnColor = tempArguments.getInt("cancelBtnColor")
        cancelBtn.setTextColor(cancelBtnColor)

        val neutralBtnLabel = tempArguments.getString("neutralBtnLabel")
        if(StringUtils.isNotEmpty(neutralBtnLabel)){
            neutralBtn.text = neutralBtnLabel
        } else{
            neutralBtn.visibility = View.GONE
        }

        val neutralBtnColor = tempArguments.getInt("neutralBtnColor")
        neutralBtn.setTextColor(neutralBtnColor)

        val confirmBtnLabel = tempArguments.getString("confirmBtnLabel")
        if(StringUtils.isNotEmpty(confirmBtnLabel)){
            confirmBtn.text = confirmBtnLabel
        } else{
            confirmBtn.visibility = View.GONE
        }

        val confirmBtnColor = tempArguments.getInt("confirmBtnColor")
        confirmBtn.setTextColor(confirmBtnColor)
    }

    override fun setSize(width: Int, height: Int) {
        super.setSize(UIUtils.getDisplayWidth() - UIUtils.dip2Px(30) * 2, height)
    }

    override fun setLocation(gravity: Int) {
        super.setLocation(Gravity.CENTER)
    }
}